<?php

namespace App\Tables;

use Illuminate\Database\Eloquent\Model;

class UserAgentExtend extends Model
{
    protected $table = 'user_agent_extend';

    protected $casts = [
        'prorations' => 'json'
    ];

    protected $appends = [
        'agent_level_text',
        'parent_agent_name',
        'operation_user_name',
        'audit_status_text'
    ];

    /**
     * 格式化代理级别显示
     * @return string
     */
    public function getAgentLevelTextAttribute()
    {
        return $this->agent_level . '级代理';
    }

    /**
     * 获取父级代理名称
     */
    public function getParentAgentNameAttribute()
    {
        $parent = $this->parent()->first();
        if (empty($parent)) {
            return '';
        }
        return $parent->realname;
    }

    /**
     * 格式化审核状态
     * @return mixed|string
     */
    public function getAuditStatusTextAttribute()
    {
        switch ($this->audit_status) {
            case 'wait_audit':
                return '待审核';
            case 'approved':
                return '已审核';
            case 'rejection':
                return '已拒绝';
            default:
                return $this->audit_status;
        }
    }

    /**
     * 获取添加人名称
     */
    public function getOperationUserNameAttribute()
    {
        $parent = $this->operationUser()->first();
        if (empty($parent)) {
            return '';
        }
        return $parent->realname;
    }

    /**
     * 获取父级代理
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo('App\Tables\User', 'parent_user_id', 'id');
    }

    /**
     * 获取添加人
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function operationUser()
    {
        return $this->belongsTo('App\Tables\User', 'operation_user_id', 'id');
    }
}
